<?php
class Groupsdb extends Model{
	function Groupsdb(){
		parent::Model();
	}
	function getAllGroups($offset=0,$limit=10){
		$return = array();
    	$query = "SELECT * FROM groups ORDER BY id DESC LIMIT $limit OFFSET $offset";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return['id'] []  = $row->id;
				$return ['groupname'] [] = $row->groupname;
				$return ['title'] [] = $row->title;
				$return ['avatar'] [] = $row->avatar;
				$return ['about_me'] [] = $row->about_me;
				$return ['num_followers'] [] = $row->num_followers;
			}
			return $return;
    	}
    	return FALSE;
	}
	function createGroup($groupname,$namagroup,$deskripsi,$user_id){
		$query = "INSERT INTO groups (groupname,title,about_me,num_followers) VALUES ('$groupname','$namagroup','$deskripsi','1')";
    	if($this->db->query($query)){
    		$gid = $this->db->insert_id();
    		$query = "INSERT INTO groups_admins (group_id,user_id) VALUES('$gid','$user_id')";
    		if($this->db->query($query)){
    			$query = "INSERT INTO groups_followed (user_id,group_id,date) VALUES('$user_id','$gid','".time()."')";
    			return $this->db->query($query);
    		}
    	}
    	return FALSE;
	}
	function getGroup($groupname){
		$return = array();
    	$query = "SELECT * FROM groups WHERE groupname = '$groupname'";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return['id']  = $row->id;
				$return ['groupname']  = $row->groupname;
				$return ['title'] = $row->title;
				$return ['avatar']  = $row->avatar;
				$return ['about_me'] = $row->about_me;
				$return ['num_followers']  = $row->num_followers;
			}
			return $return;
    	}
    	return FALSE;
	}
	function isMyGroup($user_id,$group_id){
    	$query = "SELECT * FROM groups_followed WHERE group_id = '$group_id' AND user_id='$user_id' LIMIT 1 ";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
			return TRUE;
    	}
    	return FALSE;
	}
	function joinGroup($user_id,$group_id){
		$query = "SELECT num_followers FROM groups WHERE id = '$group_id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$num_followers  = $row->num_followers;
			}
	   	 	$query = "UPDATE groups SET num_followers='".($num_followers+1)."' WHERE id = '$group_id'";
		    if($this->db->query($query)){
		    	$query = "INSERT INTO groups_followed (user_id,group_id,date) VALUES('$user_id','$group_id','".time()."')";
		    	return $this->db->query($query);
		    }
    	}
    	return FALSE;
	}
	function isGroupAdmin($user_id,$group_id){
		$query = "SELECT * FROM groups_admins WHERE group_id = '$group_id' AND user_id='$user_id' LIMIT 1 ";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
			return TRUE;
    	}
    	return FALSE;
	}
	
	function leaveGroup($user_id,$group_id){
		$query = "SELECT num_followers FROM groups WHERE id = '$group_id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$num_followers  = $row->num_followers;
			}
	   	 	$query = "UPDATE groups SET num_followers='".($num_followers-1)."' WHERE id = '$group_id'";
		    if($this->db->query($query)){
		    	$query = "DELETE FROM groups_followed WHERE user_id = '$user_id' AND group_id= '$group_id'";
		    	return $this->db->query($query);
		    }
    	}
    	return FALSE;
	}
	function adminLeaveGroup($group_id){
		$query = "DELETE FROM groups_followed WHERE group_id= '$group_id'";
		if($this->db->query($query)){
			$query = "DELETE FROM groups_admins WHERE group_id= '$group_id'";
			if($this->db->query($query)){
				$query = "DELETE FROM groups WHERE id= '$group_id'";
				return $this->db->query($query);
			}
		}
		return FALSE;
	}
	function getMembersGroup($group_id){
		$return = FALSE;
		$query = "SELECT * FROM groups_followed WHERE group_id = '$group_id' ORDER BY id ASC ";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
   	 			$return ['id'] [] = $row->user_id;
   	 		}
    	}
    	return $return;
	}
	
	function updateProfile($group_id,$title,$about_me){
		$query = "UPDATE groups SET title='$title',about_me='$about_me' WHERE id='$group_id'";
		return $this->db->query($query);
	}
	
	function updateAvatar($group_id,$avatar){
		$query = "UPDATE groups SET avatar='$avatar' WHERE id='$grou